//
//  MNDTransferHeaderView.m
//  minidai
//
//  Created by 谭谭伟 on 17/5/18.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDTransferHeaderView.h"
#import "UIImageView+JKAddition.h"
@interface MNDTransferHeaderView ()
@property (strong, nonatomic) UIButton *expandButton;
@property (strong, nonatomic) UIImageView *expandImage;
@end

@implementation MNDTransferHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithReuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self initialContents];
        self.contentView.backgroundColor = [UIColor whiteColor];
    }
    
    return self;
}

- (UILabel *)transferStatusLabel {
    if (!_transferStatusLabel) {
        _transferStatusLabel = [self labelWithDefaultText:@"1-转让成功" textColor:kColorBlack fontSize:14.0];
        [self.contentView addSubview:_transferStatusLabel];
    }
    return _transferStatusLabel;
}

- (UIImageView *)expandImage {
    if (!_expandImage) {
        _expandImage = [UIImageView jk_imageViewWithImageNamed:@"investment_detail_expand"];
        
        [self.contentView addSubview:_expandImage];
    }
    
    return _expandImage;
}


//button添加点击事件
- (UIButton *)expandButton {
    if (!_expandButton) {
        _expandButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_expandButton addTarget:self action:@selector(expandAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_expandButton];
    }
  
    return _expandButton;
}


- (void)initialContents {
    CGFloat edgeMargin = 15.0;
    [self.transferStatusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView).offset(edgeMargin);
        make.centerY.equalTo(self.contentView);
    }];
    [self.expandButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    [self.expandImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.contentView).offset(-edgeMargin);
        make.centerY.equalTo(self.contentView);
    }];

}


- (void)expandAction:(UIButton *)sender {
    
    self.model.isSectionExpand = !self.model.isSectionExpand;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        if (self.model.isSectionExpand) {
            
            self.expandImage.transform = CGAffineTransformIdentity;
            
        }else{
            
            self.expandImage.transform = CGAffineTransformMakeRotation(M_PI);
        }
        
    }];
    if (self.headerClickBlock) {
        self.headerClickBlock(YES);
    }

}

- (void)setModel:(MNDInvestmentSectionModel *)model {
    _model = model;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        if (self.model.isSectionExpand) {
            
            self.expandImage.transform = CGAffineTransformIdentity;
            
        }else{
            
            self.expandImage.transform = CGAffineTransformMakeRotation(M_PI);
        }
        
    }];
}

- (UILabel *)labelWithDefaultText:(NSString *)text textColor:(UIColor *)color fontSize:(CGFloat)size {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.text = text;
    label.textColor = color;
    label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:size];
    
    return label;
}
@end
